package db

import (
	"fmt"

	"commonUtils/config"
	"commonUtils/utils"

	_ "github.com/lib/pq"
	xorm "xorm.io/xorm"
)

func NewPostgresEngine(conf *config.DBConfig) *xorm.Engine {
	constr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", conf.Addr, conf.Port, conf.User, conf.Pass, conf.DBName) //"postgres://" + conf.User + ":" + conf.Pass + "@" + conf.Addr + ":" + conf.Port + "/" + conf.DBName + "?sslmode=disable"
	utils.Debug("constr : %s", constr)
	engine, err := xorm.NewEngine("postgres", constr)
	if err != nil {
		panic(err.Error())
	}

	//设置显示sql语句
	// if config.IsDebug() {
	// 	engine.ShowSQL(true)
	// }
	engine.SetMaxOpenConns(10)

	return engine
}
